package com.lanou.dllo.soccernews.database;

import com.lanou.dllo.soccernews.application.BaseApplication;
import com.lanou.dllo.soccernews.bean.greendao.DaoSession;
import com.lanou.dllo.soccernews.bean.greendao.Users;
import com.lanou.dllo.soccernews.bean.greendao.UsersDao;

import java.util.List;

import de.greenrobot.dao.query.QueryBuilder;

public class UsersDBhelper {
    private static UsersDBhelper userHelper;
    private static DaoSession daoSession;

    private UsersDBhelper() {

    }

    public static UsersDBhelper getInstance() {

        if (userHelper == null) {
            userHelper = new UsersDBhelper();
            daoSession = BaseApplication.getDaoSession();
        }

        return userHelper;

    }

    public List<Users> getUsersInfoList() {
        QueryBuilder<Users> qb = daoSession.getUsersDao().queryBuilder();

        return qb.list();
    }



    public  void updateInfo(Users users) {
        daoSession.insertOrReplace(users);
    }

    //注册---插入
    public void addToUserInfoTable(Users item) {
        //daoSession.getUsersDao().insert(item);
        daoSession.getUsersDao().insertInTx(item);
    }

    //登录---

    public String getLoginInfo(String name) {

        QueryBuilder<Users> qb = daoSession.getUsersDao().queryBuilder();

        qb.whereOr(UsersDao.Properties.Username.eq(name),UsersDao.Properties.Tel.eq(name));
        if (qb.list().size() > 0) {
            return qb.list().get(0).getPassword();
        } else {
            return null;
        }
    }
    public List<Users> getUsesToTel(String tel) {
        QueryBuilder<Users> qb = daoSession.getUsersDao().queryBuilder();
        qb.where(UsersDao.Properties.Tel.eq(tel));
        if (qb.list().size() > 0) {
            return qb.list();
        } else {
            return null;
        }
    }

    //注册 -- 姓名
    public boolean getName(String name) {
        QueryBuilder<Users> qb = daoSession.getUsersDao().queryBuilder();
        qb.where(UsersDao.Properties.Username.eq(name));

        return qb.buildCount().count() > 0 ? false : true;

    }

    //注册  -- 手机号
    public boolean getTel(String tel) {
        QueryBuilder<Users> qb = daoSession.getUsersDao().queryBuilder();
        qb.where(UsersDao.Properties.Tel.eq(tel));

        return qb.buildCount().count() > 0 ? false : true;

    }

    public void deleteOne() {
        daoSession.getUsersDao().deleteAll();
    }


    //插入

    public void addUserInfo(Users users){
        daoSession.getUsersDao().insert(users);

    }
}
